package com.bgrc.hubu12305.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgrc.hubu12305.dao.ParkStationVODao;
import com.bgrc.hubu12305.dao.ParkingStationDao;
import com.bgrc.hubu12305.dao.SeatDao;
import com.bgrc.hubu12305.dao.TrainQueryDao;
import com.bgrc.hubu12305.entity.ParkingStationEntity;
import com.bgrc.hubu12305.service.TrainQueryService;
import com.bgrc.hubu12305.vo.ParkingVO;
import com.bgrc.hubu12305.vo.TrainQueryReturnVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 包名:com.bgrc.hubu12305.service.impl
 * 用户:86182
 * 时间:20211223:562021/12/15
 */
@Service
public class TrainQueryServiceImpl extends ServiceImpl<TrainQueryDao,TrainQueryReturnVO> implements TrainQueryService{
    @Autowired
    private ParkingStationDao parkingStationDao;

    @Autowired
    private SeatDao seatDao;

    @Autowired
    private ParkStationVODao parkStationVODao;

    @Override
    public List<TrainQueryReturnVO> queryTrainList(String startStation, String endStation) {
        //通过出发站得到所有满足的车次信息
        List<ParkingStationEntity> startStationInfo = parkingStationDao.queryStartStationList(startStation);

        List<TrainQueryReturnVO> returnVOList = new ArrayList<>();

        for (ParkingStationEntity parking:startStationInfo) {
            Integer time = parking.getRunningTime();
            //排除不停靠的站
            if ((!parking.getArriveTime().equals(parking.getStartTime()))||time==0) {
                String trainNumber = parking.getTrainNumber();
                Integer distance = parking.getStartStationDistance();
                //查询满足的车次号情况下所有满足到达站的信息
                ParkingStationEntity endStationR = parkingStationDao.queryEndStationList(trainNumber, endStation,distance);

                if(endStationR!=null){
                    TrainQueryReturnVO returnVO = new TrainQueryReturnVO();
                    //列车编号
                    returnVO.setTrainNumber(parking.getTrainNumber());
                    //出发站
                    returnVO.setStartStation(parking.getStationName());
                    //到达站
                    returnVO.setEndStation(endStationR.getStationName());
                    //到站时间
                    returnVO.setArriveTime(parking.getArriveTime());
                    //发车时间
                    returnVO.setStartTime(parking.getStartTime());
                    //两站间距
                    returnVO.setDistance(endStationR.getStartStationDistance()-parking.getStartStationDistance());
                    //运行时长
                    Integer runningTime = endStationR.getRunningTime()-parking.getRunningTime();
                    returnVO.setRunningTime(runningTime);
                    //查询不同等级座位的座位数
                    String trainNo=parking.getTrainNo();
                    Integer firstSeat = seatDao.querySeatCountByType(trainNo, 1);
                    Integer secondSeat = seatDao.querySeatCountByType(trainNo, 2);
                    Integer softSleeper = seatDao.querySeatCountByType(trainNo, 3);
                    Integer hardSleeper = seatDao.querySeatCountByType(trainNo, 4);

                    returnVO.setFirstSeat(firstSeat);
                    returnVO.setSecondSeat(secondSeat);
                    returnVO.setSoftSleeper(softSleeper);
                    returnVO.setHardSleeper(hardSleeper);

                    //查询经停站信息
                    QueryWrapper<ParkingStationEntity> queryWrapper = new QueryWrapper<>();

                    queryWrapper.eq("train_number",parking.getTrainNumber());

                    List<ParkingStationEntity> stationList = parkingStationDao.selectList(queryWrapper);
                    List<ParkingVO> parkingVOS = new ArrayList<>();
                    int i=0;

                    for (ParkingStationEntity station:stationList
                         ) {
                        i++;
                        ParkingVO parkingVO = new ParkingVO();
                        parkingVO.setStationNo(i);
                        parkingVO.setStationName(station.getStationName());
                        parkingVO.setArriveTime(station.getArriveTime());
                        parkingVO.setStartTime(station.getStartTime());
                        /*String arriveTime = station.getArriveTime();
                        String startTime = station.getStartTime();*/
                        long parkingTime=0L;
                        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
                        try {
                            long arriveTime = dateFormat.parse(station.getArriveTime()).getTime();
                            long startTime = dateFormat.parse(station.getStartTime()).getTime();
                            parkingTime = (startTime-arriveTime)/60000;
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        parkingVO.setParkingTime(String.valueOf(parkingTime));
                        parkingVOS.add(parkingVO);
                    }
                    returnVO.setParkStationList(parkingVOS);

                    returnVOList.add(returnVO);
                }
            }
        }
        return returnVOList;
    }
}
